package com.sendpo.zjx.sendpo.activity;

import android.graphics.Bitmap;
import android.location.Location;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.UiSettings;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.MyLocationStyle;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.sendpo.zjx.sendpo.R;
import com.sendpo.zjx.sendpo.utils.Tools;

import org.xutils.common.util.DensityUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Administrator on 2017/11/9.
 */

public class MerchantMapActivity extends AppCompatActivity implements AMap.OnMyLocationChangeListener {

    @BindView(R.id.backbt)
    RelativeLayout backbt;
    @BindView(R.id.index_head)
    RelativeLayout indexHead;
    @BindView(R.id.mapview)
    MapView mapview;
    @BindView(R.id.cir_icon)
    CircleImageView cirIcon;
    @BindView(R.id.uname)
    TextView uname;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.price_tv)
    TextView priceTv;
    @BindView(R.id.textcontent)
    TextView textcontent;
    @BindView(R.id.userinfo_bt)
    Button userinfoBt;
    @BindView(R.id.userlistitem)
    LinearLayout userlistitem;
    private AMap aMap=null;
    public String userLongitude ="";
    public String userLatitude ="";
    public String userProvince = "";
    public String userCity = "";
    public String userArea = "";
    public String userAddress = "";
    private String  userId="";
   private  String merchatName="";
    private  String merchantsId="";
    private  String merchantsAvatarpath="";
    public boolean isFirst=true;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_merchantmap);
        Tools.initStatus(this, R.color.black);
        ButterKnife.bind(this);
        mapview.onCreate(savedInstanceState);
        if(aMap==null){
            aMap=mapview.getMap();
        }
        initComponent();
        userLongitude=getIntent().getStringExtra("userLongitude");
        userLatitude=getIntent().getStringExtra("userLatitude");
        merchatName=getIntent().getStringExtra("merchatName");
        userId=getIntent().getStringExtra("userId");
        merchantsId=getIntent().getStringExtra("merchantsId");
        merchantsAvatarpath=getIntent().getStringExtra("merchantsAvatarpath");
        LatLng latLng = new LatLng(Double.parseDouble(userLatitude),Double.parseDouble(userLongitude));

        MarkerOptions markerOptions=new MarkerOptions();
        //是否可以拖拽定位点
        markerOptions.draggable(false);
        //设置好经度，纬度坐标
        //设置好坐标标题
        markerOptions.title(merchatName+"");
        //设置定位点的显示效果
        markerOptions.setFlat(true);//设置marker平贴地图效果
        markerOptions.infoWindowEnable(false);
        markerOptions.setGps(false);
        final Marker marker=aMap.addMarker(markerOptions);
        marker.setPosition(latLng);
        marker.setIcon(BitmapDescriptorFactory.fromView(View.inflate(this, R.layout.circle_views, null)));
        aMap.addMarker(markerOptions);

        Glide.with(getApplicationContext())
                .load(merchantsAvatarpath+"")
                .asBitmap()
                .animate(R.anim.fade_in)
                .override(DensityUtil.dip2px(35), DensityUtil.dip2px(35))
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                        View view = View.inflate(MerchantMapActivity.this, R.layout.circle_views, null);
                        ((CircleImageView) view.findViewById(R.id.cir_icon))
                                .setImageBitmap(resource);
                        marker.setIcon(BitmapDescriptorFactory.fromView(view));
                    }
                });
    }

    private void initComponent() {
        final UiSettings uiSettings =  aMap.getUiSettings();
        uiSettings.setLogoBottomMargin(-50);//隐藏logo
        //开启室内地图
        aMap.showIndoorMap(true);
        //是否开启指南针
        aMap.getUiSettings().setCompassEnabled(false);
        //是否开启定位按钮
        aMap.setMyLocationEnabled(true);
        aMap.getUiSettings().setMyLocationButtonEnabled(false); //显示默认的定位按钮
        aMap.getUiSettings().setScaleControlsEnabled(true);//控制比例尺控件是否显示
        MyLocationStyle myLocationStyle= new MyLocationStyle();//初始化定位蓝点样式类;
        myLocationStyle.showMyLocation(true);
        //  MyLocationStyle strokeColor();//设置定位蓝点精度圆圈的边框颜色的方法。
        //设置定位蓝点精度圆圈的填充颜色的方法。
        // myLocationStyle.radiusFillColor(getResources().getColor(R.color.default_theme));
        myLocationStyle.radiusFillColor(getResources().getColor(R.color.translucent));
        myLocationStyle.strokeWidth(0.0f);
        myLocationStyle.strokeColor(getResources().getColor(R.color.translucent));
        myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE_NO_CENTER);//连续定位、且将视角移动到地图中心点，定位点依照设备方向旋转，并且会跟随设备移动。（1秒1次定位）如果不设置myLocationType，默认也会执行此种模式。
        myLocationStyle.interval(1000); //设置连续定位模式下的定位间隔，只在连续定位模式下生效，单次定位模式下不会生效。单位为毫秒。
        aMap.setMyLocationStyle(myLocationStyle);//设置定位蓝点的Style
        //aMap.getUiSettings().setMyLocationButtonEnabled(true);设置默认定位按钮是否显示，非必需设置。
       // aMap.moveCamera(CameraUpdateFactory.zoomTo(18));
        aMap.setMyLocationEnabled(true);// 设置为true表示启动显示定位蓝点，false表示隐藏定位蓝点并不进行定位，默认是false。
        //J监听点击事件
        aMap.setOnMyLocationChangeListener(this);
        aMap.setTrafficEnabled(true);
        aMap.setMapType(AMap.MAP_TYPE_NORMAL);
        aMap.setOnMarkerClickListener(new AMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {
                Map<String,Object> map=new HashMap<>();
                map.put("id",Integer.parseInt(userId));
                map.put("merchantsId",Integer.parseInt(merchantsId));
                Tools.redirectActivity(MerchantMapActivity.this,StoreMainAllProduceActivity.class,map);
                return false;
            }
        });


    }

    @OnClick(value = R.id.backbt)
    public  void onMyClick(View view){
         switch (view.getId()){
             case R.id.backbt:
                 finish();
                 break;
         }
    }

    @Override
    protected void onResume() {
        super.onResume();
        mapview.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mapview.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapview.onDestroy();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapview.onSaveInstanceState(outState);
    }

    @Override
    public void onMyLocationChange(Location location) {
            if(location==null){
                return;
            }
            if(isFirst==true){
                aMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
                        new LatLng(location.getLatitude(),location.getLongitude()),21));
                isFirst=false;
            }else{
            }


    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }
}
